package com.member.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.member.common.utils.DateUtils;
import com.member.shop.domain.SysBizAgreement;
import com.member.shop.domain.SysBizGoods;
import com.member.shop.domain.SysBizGoodsSku;
import com.member.shop.domain.SysBizWithdrawApply;
import com.member.shop.mapper.SysBizUserMapper;
import com.member.shop.mapper.SysBizWithdrawApplyMapper;
import com.member.shop.service.ISysBizAgreementService;
import com.member.shop.service.ISysBizGoodsService;
import com.member.shop.service.ISysBizGoodsSkuService;
import com.member.shop.service.ISysBizWithdrawApplyService;
import com.member.shop.vo.DevelopGoodsVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * 提现申请Service业务层处理
 *
 * @author lixinchen
 * @date 2024-01-11
 */
@Service
public class SysBizWithdrawApplyServiceImpl extends ServiceImpl<SysBizWithdrawApplyMapper, SysBizWithdrawApply> implements ISysBizWithdrawApplyService {
    @Resource
    private SysBizWithdrawApplyMapper sysBizWithdrawApplyMapper;
    @Resource
    private ISysBizAgreementService sysBizAgreementService;
    @Resource
    private ISysBizGoodsService sysBizGoodsService;
    @Resource
    private ISysBizGoodsSkuService sysBizGoodsSkuService;

    @Resource
    private SysBizUserMapper sysBizUserMapper;

    /**
     * 查询提现申请
     *
     * @param id 提现申请主键
     * @return 提现申请
     */
    @Override
    public SysBizWithdrawApply selectSysBizWithdrawApplyById(Long id) {
        return sysBizWithdrawApplyMapper.selectById(id);
    }

    /**
     * 查询提现申请列表
     *
     * @param queryWrapper 提现申请
     * @return 提现申请
     */
    @Override
    public List<SysBizWithdrawApply> selectSysBizWithdrawApplyList(LambdaQueryWrapper<SysBizWithdrawApply> queryWrapper) {
        return sysBizWithdrawApplyMapper.selectList(queryWrapper);
    }

    /**
     * 查询提现申请列表
     *
     * @param sysBizWithdrawApply 提现申请
     * @return 提现申请
     */
    @Override
    public List<SysBizWithdrawApply> selectSysBizWithdrawApplyList(SysBizWithdrawApply sysBizWithdrawApply) {
        return sysBizWithdrawApplyMapper.selectSysBizWithdrawApplyList(sysBizWithdrawApply);
    }

    /**
     * 查询提现申请列表(统计)
     *
     * @param sysBizWithdrawApply 提现申请
     * @return 提现申请集合
     */
    @Override
    public List<SysBizWithdrawApply> withdrawalCount(SysBizWithdrawApply sysBizWithdrawApply) {
        return sysBizWithdrawApplyMapper.withdrawalCount(sysBizWithdrawApply);
    }

    /**
     * 新增提现申请
     *
     * @param sysBizWithdrawApply 提现申请
     * @return 结果
     */
    @Override
    public int insertSysBizWithdrawApply(SysBizWithdrawApply sysBizWithdrawApply) {
        sysBizWithdrawApply.setCreateTime(DateUtils.getNowDate());
        return sysBizWithdrawApplyMapper.insert(sysBizWithdrawApply);
    }

    /**
     * 批量删除提现申请
     *
     * @param ids 需要删除的提现申请主键
     * @return 结果
     */
    @Override
    public int deleteSysBizWithdrawApplyByIds(Long[] ids) {
        return sysBizWithdrawApplyMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除提现申请信息
     *
     * @param id 提现申请主键
     * @return 结果
     */
    @Override
    public int deleteSysBizWithdrawApplyById(Long id) {
        return sysBizWithdrawApplyMapper.deleteById(id);
    }

    @Override
    public DevelopGoodsVo queryDevelop() {
        SysBizAgreement sysBizAgreement = sysBizAgreementService.selectSysBizAgreementById(1L);
        DevelopGoodsVo developGoodsVo = new DevelopGoodsVo();
        developGoodsVo.setText(sysBizAgreement.getCourtshipInfo());
        developGoodsVo.setGoodsId(sysBizAgreement.getCourtshipShopId());
        developGoodsVo.setCourtshipAmount(sysBizAgreement.getCourtshipAmount());
        if (sysBizAgreement.getCourtshipShopId() == null) {
            developGoodsVo.setCover(sysBizAgreement.getMemberPic());
            return developGoodsVo;
        }
        SysBizGoods sysBizGoods = sysBizGoodsService.getById(sysBizAgreement.getCourtshipShopId());
        List<SysBizGoodsSku> sysBizGoodsSkus = sysBizGoodsSkuService.selectSysBizGoodsSkuList(new LambdaQueryWrapper<SysBizGoodsSku>()
                .eq(SysBizGoodsSku::getGoodsId, sysBizGoods.getId()).eq(SysBizGoodsSku::getStatus, 0));
        developGoodsVo.setPic(sysBizGoods.getCover());
        if (!CollectionUtils.isEmpty(sysBizGoodsSkus)) {
            sysBizGoodsSkus.sort(Comparator.comparing(SysBizGoodsSku::getMemberSalesPrice));
            developGoodsVo.setPic(sysBizGoodsSkus.get(0).getPic());
        }
        developGoodsVo.setGoodsName(sysBizGoods.getName());
        developGoodsVo.setCover(sysBizGoods.getCover());
        return developGoodsVo;
    }
}
